﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RentalDTO;
using RentalIService;
using RentalService.Entities;

namespace RentalService
{
    public class ProcessTemplateService : IServiceProcessTemplate
    {
        public int AddProcessTemplate(string Id, string Type, string RecordRemarks, int StepOrder, string WaitForExecutionStaffId, DateTime CreateTime, DateTime UpdateTime, int? IsDel)
        {
            using (var db = new RentalContext())
            {
                BaseService<ProcessTemplate> bs = new BaseService<ProcessTemplate>(db);

                ProcessTemplate Template = new ProcessTemplate();
                Template.Id = Id;
                Template.Type = Type;
                Template.RecordRemarks = RecordRemarks;
                Template.StepOrder = StepOrder;
                Template.WaitForExecutionStaffId = WaitForExecutionStaffId;
                Template.CreateTime = CreateTime;
                Template.UpdateTime = UpdateTime;
                Template.IsDel = IsDel;

                if (bs.Add(Template))
                {

                    return 1;
                }
                else
                {
                    return 0;
                }
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelProcessTemplate(string id)
        {
            using (var db = new RentalContext())
            {
                int a = 0;
                BaseService<ProcessTemplate> bs = new BaseService<ProcessTemplate>(db);
                var list = bs.GetLists(u => u.Type == id).ToList();
                foreach (var item in list)
                {
                    db.ProcessTemplate.Remove(item);
                    if (db.SaveChanges()>0) {
                        a += 1;
                    }
                }

                if (list.Count()==a)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
        }

        public List<ProcessTemplateDTO> select(string id)
        {
            using (var db = new RentalContext())
            {
                BaseService<ProcessTemplate> bs = new BaseService<ProcessTemplate>(db);
                var list = (from a in db.ProcessTemplate
                            join b in db.Staff
                            on a.WaitForExecutionStaffId equals b.Id
                            where a.Type == id
                            orderby a.StepOrder ascending
                            select new ProcessTemplateDTO
                            {
                                Id = a.Id,
                                Type = a.Type,
                                RecordRemarks = a.RecordRemarks,
                                StepOrder = a.StepOrder,
                                WaitForExecutionStaffId = a.WaitForExecutionStaffId,
                                staffName = b.Name
                            }).ToList();
                return list;
            }
        }
    }
}
